package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopOrder;
import com.bdqn.pojo.ShopOrderCountVO;
import com.bdqn.service.ShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="订单接口")
@RestController
@RequestMapping("/shoporder")
public class ShopOrderController {

    @Autowired
    private ShopOrderService shopOrderService;

    /**
     * 新增订单记录
     *
     * @param shopOrder
     * @return R
     */
    @ApiOperation("新增订单记录")
    @WebLogger("新增订单记录")
    @PostMapping("/add")
    public R save(@ApiParam("订单对象") @RequestBody ShopOrder shopOrder) {
        return new R<>(shopOrderService.save(shopOrder));
    }

    /**
     * 修改订单记录
     *
     * @param shopOrder
     * @return R
     */
    @ApiOperation("修改订单记录")
    @WebLogger("修改订单记录")
    @PostMapping("/update")
    public R update(@ApiParam("订单对象") @RequestBody ShopOrder shopOrder) {
        return new R<>(shopOrderService.updateById(shopOrder));
    }

    /**
     * 通过id删除一条订单记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条订单记录")
    @WebLogger("通过id删除一条订单记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("订单id") Integer id) {
        return new R<>(shopOrderService.removeById(id));
    }

    /**
     * 通过id查询单条订单记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条订单记录")
    @WebLogger("通过id查询单条订单记录")
    @GetMapping("/{id}")
    public R<ShopOrder> getById(@ApiParam("订单id") @PathVariable Integer id) {
        return new R<>(shopOrderService.getById(id));
    }

    /**
     * 通过订单信息查询订单记录
     *
     * @param shopOrder 订单信息对象
     * @return R
     */
    @ApiOperation("通过订单信息查询订单记录")
    @WebLogger("通过订单信息查询订单记录")
    @GetMapping("/list")
    public R<List<ShopOrder>> getList(@ApiParam("查询的订单对象") ShopOrder shopOrder) {
        return new R<>(shopOrderService.getList(shopOrder));
    }

    /**
     * 分页查询订单
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopOrder 订单
     * @return
     */
    @ApiOperation("分页查询订单")
    @WebLogger("分页查询订单")
    @GetMapping("/page")
    public R<IPage<ShopOrder>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的订单对象") ShopOrder shopOrder) {
        return new R<>(shopOrderService.getPage(new Page<>(pageNo, pageSize), shopOrder));
    }

    /**
     * 分页查询订单数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopOrder 订单
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询订单数据")
    @WebLogger("分页查询订单数据")
    @GetMapping("/pagelist")
    public R<List<ShopOrder>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的订单对象") ShopOrder shopOrder) {
        IPage<ShopOrder> page = shopOrderService.getPage(new Page<>(pageNo, pageSize), shopOrder);
        return new R<>(page.getRecords(), page.getTotal());
    }


    /**
     * 用户数统计
     *
     * @return
     */
    @ApiOperation("订单数统计")
    @WebLogger("订单数统计")
    @GetMapping("/countRate")
    public R<ShopOrderCountVO> getCountRate() {

        return new R<>(shopOrderService.getCountRate());
    }
}
